package com.yanqu.road.server.cross.service;

import com.google.protobuf.AbstractMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.command.ICrossServiceCommand;
import com.yanqu.road.server.manager.union.CrossUnionMgr;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

public class CrossGetUnionMemberUserIdServiceCmd implements ICrossServiceCommand {

    @Override
    public CompletableFuture<AbstractMessage.Builder<?>> execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        CrossUnionProto.CrossGetUnionMemberIdRespMsg.Builder respMsg = CrossUnionProto.CrossGetUnionMemberIdRespMsg.newBuilder();
        CrossUnionProto.CrossGetUnionMemberIdReqMsg reqMsg = CrossUnionProto.CrossGetUnionMemberIdReqMsg.parseFrom(request.getDataBytes());
        for (String unionUid : reqMsg.getUnionUidList()) {
            Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
            if(unionMemberMap == null){
                continue;
            }
            CrossUnionProto.CrossUnionMemberListTemp.Builder builder = CrossUnionProto.CrossUnionMemberListTemp.newBuilder();
            builder.setUnionUid(unionUid);
            for (Map.Entry<Long, CrossUnionMember> memberEntry : unionMemberMap.entrySet()) {
                builder.addUserId(memberEntry.getKey());
            }
            respMsg.addMember(builder);
        }
        CompletableFuture<AbstractMessage.Builder<?>> completableFuture = new CompletableFuture<>();
        completableFuture.complete(respMsg);
        return completableFuture;
    }
}
